package com.order.api.service.impl;

import com.order.api.dao.ShopsDao;
import com.order.api.po.Shops;
import com.order.api.po.ShopsExample;
import com.order.api.service.ShopsService;
import com.order.api.vo.request.*;
import com.order.api.vo.response.ShopsResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class ShopsServiceImpl implements ShopsService {

    @Autowired
    private ShopsDao shopsDao;

    @Autowired
    private RedisTemplate  redisTemplate;

    @Override
    public List<ShopsResponse> findShops() {
        ValueOperations<String, Shops> operations=redisTemplate.opsForValue();
        ShopsExample example = new ShopsExample();
        List<Shops> shops = shopsDao.selectByExample(example);
        List<ShopsResponse> list=new ArrayList<>();
        for (Shops shop : shops) {
            ShopsResponse response = new ShopsResponse();
            BeanUtils.copyProperties(shop,response);
            list.add(response);
        }
        return list;
    }

    @Override
    public ShopsResponse findDetail(ShopsIdRequest request) {
        //判断请求参数是否为空
        if (null==request){
            return new ShopsResponse();
        }
        //设置redis的key值，需保证redis中的key都类似
        String key="shop_" +request.getId();
        ValueOperations<String,Shops> operations=redisTemplate.opsForValue();
        //判断redis中是否存在key
        boolean hasKey=redisTemplate.hasKey(key);
        if (hasKey){
            Shops shops= operations.get(key);
            ShopsResponse response = new ShopsResponse();
            BeanUtils.copyProperties(shops,response);

            return response;
        }else {
            Shops shops = shopsDao.selectByPrimaryKey(request.getId());
            if (shops==null){
                return null;
            }
            operations.set(key,shops,5, TimeUnit.HOURS);
            return ShopsResponse.builder()
                    .commodityName(shops.getCommodityName())
                    .commodityPrice(shops.getCommodityPrice())
                    .commodityQuantity(shops.getCommodityQuantity())
                    .id(shops.getId())
                    .build();
        }



    }

    @Override
    public boolean addShops(ShopsAddRequest request) {
        //判断参数
        if (null==request){
            return false;
        }
        ValueOperations<String, Shops> operations=redisTemplate.opsForValue();
        Shops shops = new Shops();
        BeanUtils.copyProperties(request,shops);
        int count = shopsDao.insert(shops);
        if (count != 0){
            String key = "shop_"+shops.getId();
            operations.set(key,shops,5, TimeUnit.HOURS);
        }

        return count == 1;
    }

    @Override
    public boolean updateShops(ShopsUpdateRequest request) {
        //判断参数
        if (null == request){
            return false;
        }
        //修改数据库
        ValueOperations<String, Shops> operations = redisTemplate.opsForValue();
        Shops shops = new Shops();
        BeanUtils.copyProperties(request,shops);
        int count = shopsDao.updateByPrimaryKey(shops);
        if (count!=0){
            String key="shop_"+request.getId();
            boolean hasKey = redisTemplate.hasKey(key);
            if (hasKey){
                //删除redis中的数据
                redisTemplate.delete(key);
            }
            Shops shops1 = shopsDao.selectByPrimaryKey(request.getId());
            if (shops1!=null){
                //更新redis缓存
                operations.set(key,shops1,5,TimeUnit.HOURS);
            }
        }
        return count == 1;
    }

    @Override
    public boolean deleteShops(ShopsIdRequest request) {
        //判断参数
        if (null==request){
            return false;
        }
        int count = shopsDao.deleteByPrimaryKey(request.getId());
        if (count!=0){
            String key="shop_"+request.getId();
            boolean hasKey = redisTemplate.hasKey(key);
            if (hasKey){
                redisTemplate.delete(key);
            }
        }
        return count == 1;
    }
}
